/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package store.forms;

import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author Mark
 */
public class Request extends javax.swing.JPanel {

    /**
     * Creates new form Request
     */
    public Request() {
        initComponents();
        warehouseInventory().stream().forEach((line) -> {
            String[] tokens = line.split(",");
            itemComboBox.addItem(tokens[0] +" -> " + tokens[1]);
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        itemLabel = new javax.swing.JLabel();
        itemComboBox = new javax.swing.JComboBox();
        quantityLabel = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        requestLabel = new javax.swing.JLabel();
        quantitySpinner = new javax.swing.JSpinner();

        itemLabel.setText("Item:");

        itemComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose Item.." }));
        itemComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemComboBoxActionPerformed(evt);
            }
        });

        quantityLabel.setText("Quantity:");

        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        requestLabel.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
        requestLabel.setText("Request");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(quantityLabel)
                            .addComponent(itemLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(itemComboBox, 0, 107, Short.MAX_VALUE)
                            .addComponent(quantitySpinner)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(submitButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(requestLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(95, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(requestLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemLabel)
                    .addComponent(itemComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(quantityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(quantitySpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitButton)
                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(35, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed

    }//GEN-LAST:event_cancelButtonActionPerformed

    private void itemComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_itemComboBoxActionPerformed

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        int selected = Integer.parseInt(itemComboBox.getSelectedItem().toString().split(" -> ")[0])-1;
        int max = Integer.parseInt(warehouseInventory().get(selected).split(",")[4]);
        if (!isQuantityValid()) {
            JOptionPane.showMessageDialog(this,
                    "Quantity cannot be larger than " + max);
        } else {
            JOptionPane.showMessageDialog(this, "Request has been queued. ");
        }
    }//GEN-LAST:event_submitButtonActionPerformed

    public String[] getRequest() {
        int selected = Integer.parseInt(itemComboBox.getSelectedItem().toString().split(" -> ")[0])-1;
        String name = warehouseInventory().get(selected).split(",")[1];
        int quantity = Integer.parseInt(quantitySpinner.getValue().toString());
        return new String[]{warehouseInventory().get(selected).split(",")[0],
            name, String.valueOf(quantity), "Pending"};
    }
    
    public boolean isQuantityValid() {      
        int selected = Integer.parseInt(itemComboBox.getSelectedItem().toString().split(" -> ")[0]);
        int max = Integer.parseInt(warehouseInventory().get(selected).split(",")[4]);
        return !(max < Integer.parseInt(quantitySpinner.getValue().toString()));
    }
    
    public void addSubmitActionListener(ActionListener al) {
        submitButton.addActionListener(al);
    }
    
    public void addCancelActionListener(ActionListener al) {
        cancelButton.addActionListener(al);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox itemComboBox;
    private javax.swing.JLabel itemLabel;
    private javax.swing.JLabel quantityLabel;
    private javax.swing.JSpinner quantitySpinner;
    private javax.swing.JLabel requestLabel;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables

    private static java.util.List<java.lang.String> warehouseInventory() {
        store.StoreWebService_Service service = new store.StoreWebService_Service();
        store.StoreWebService port = service.getStoreWebServicePort();
        return port.warehouseInventory();
    }
}
